package com.yunpeng.aidevice.common.controllers.configs;

import com.alibaba.fastjson.JSONObject;
import com.yunpeng.aidevice.biz.service.IRoleBizService;
import com.yunpeng.aidevice.common.constant.ConfigConstantValues;
import com.yunpeng.common.dto.Result;
import com.yunpeng.common.paging.PagingQueryDTO;
import com.yunpeng.common.util.ParametersCheckUtil;
import com.yunpeng.aidevice.common.vo.RoleDetailVO;
import com.yunpeng.aidevice.permission.service.IPermissionOuterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * RoleController
 *
 * @author : Moore(hemw)
 * @date : 2020-04-15
 */
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Resource
    private IRoleBizService roleBizService;
    @Resource
    private IPermissionOuterService permissionOuterService;

    /**
     * 新增角色
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addNew", method = RequestMethod.POST)
    public Result<Void> addNew(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String name = requestParam.getString("name");
        ParametersCheckUtil.isNotBlank(name, "角色名称为空");
        return roleBizService.addNew(name, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }


    /**
     * 移除角色
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public Result<Void> remove(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String roleCode = requestParam.getString("roleCode");
        ParametersCheckUtil.isNotBlank(roleCode, "角色代码为空");
        return roleBizService.remove(roleCode, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }


    /**
     * 角色列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Result<PagingQueryDTO> list(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String name = requestParam.getString("name");
        Integer pageSize = requestParam.getInteger("pageSize");
        Integer currentPage = requestParam.getInteger("currentPage");
        return roleBizService.list(name, pageSize, currentPage);
    }

    /**
     * 绑定权限
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "bindPermission", method = RequestMethod.POST)
    public Result<Void> bindPermission(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String roleCode = requestParam.getString("roleCode");
        String permissionCode = requestParam.getString("permissionCode");
        ParametersCheckUtil.isNotBlank(roleCode, "角色代码为空");
        ParametersCheckUtil.isNotBlank(permissionCode, "权限代码为空");
        permissionOuterService.clearWholePermissionCache(ConfigConstantValues.getLoginUserCacheDTO().getCode());
        return roleBizService.bindPermission(roleCode, permissionCode, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 解绑权限
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "unbindPermission", method = RequestMethod.POST)
    public Result<Void> unbindPermission(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String roleCode = requestParam.getString("roleCode");
        String permissionCode = requestParam.getString("permissionCode");
        ParametersCheckUtil.isNotBlank(roleCode, "角色代码为空");
        ParametersCheckUtil.isNotBlank(permissionCode, "权限代码为空");
        permissionOuterService.clearWholePermissionCache(ConfigConstantValues.getLoginUserCacheDTO().getCode());
        return roleBizService.unbindPermission(roleCode, permissionCode, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 用户绑定角色
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "userBindRole", method = RequestMethod.POST)
    public Result<Void> userBindRole(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String roleCode = requestParam.getString("roleCode");
        String userCode = requestParam.getString("userCode");
        Date startTime = requestParam.getDate("startTime");
        Date endTime = requestParam.getDate("endTime");
        ParametersCheckUtil.isNotBlank(roleCode, "角色代码为空");
        ParametersCheckUtil.isNotBlank(userCode, "用户代码为空");
        permissionOuterService.clearUserCache(userCode);
        return roleBizService.userBindRole(roleCode, userCode, startTime, endTime, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 用户解绑角色
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "userUnBindRole", method = RequestMethod.POST)
    public Result<Void> userUnBindRole(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String roleCode = requestParam.getString("roleCode");
        String userCode = requestParam.getString("userCode");
        ParametersCheckUtil.isNotBlank(roleCode, "角色代码为空");
        ParametersCheckUtil.isNotBlank(userCode, "用户代码为空");
        permissionOuterService.clearUserCache(userCode);
        return roleBizService.userUnbindRole(roleCode, userCode, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 角色详情视图
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "detail", method = RequestMethod.POST)
    public Result<RoleDetailVO> detail(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String roleCode = requestParam.getString("roleCode");
        ParametersCheckUtil.isNotBlank(roleCode, "角色代码为空");
        return roleBizService.detailInfo(roleCode);
    }
}
